package com.cheli.center.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cheli.center.entity.vo.LoginVo;
import com.cheli.center.service.ManaStaffService;
import com.cheli.center.entity.ManaStaff;
import com.cheli.center.mapper.ManaStaffMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cheli.commonutils.JwtUtils;
import com.cheli.servicebase.config.exceptionhandler.MyException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * <p>
 * 员工 服务实现类
 * </p>
 *
 * @author cheli
 * @since 2021-05-02
 */
@Service
public class ManaStaffServiceImpl extends ServiceImpl<ManaStaffMapper, ManaStaff> implements ManaStaffService {

   @Override
    public String login(LoginVo loginVo) {
        String id = loginVo.getId();
        String password = loginVo.getPassword();
       if(StringUtils.isEmpty(id) || StringUtils.isEmpty(password)){
            throw new MyException(20001,"登陆失败,不能为空");
        }

       QueryWrapper<ManaStaff> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        ManaStaff manaStaff = baseMapper.selectOne(wrapper);
        if(manaStaff == null){
            throw new MyException(20001, "ID或密码错误");
        }
       password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!password.equals(manaStaff.getPassword())){
            throw new MyException(20001, "ID或密码错误");
        }
       //生成token字符串
        return JwtUtils.getJwtToken(manaStaff.getId(), manaStaff.getName());
    }

}
